<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 用户信息
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-tabs v-model="message">
                <el-tab-pane :label="`全部用户`" name="first">
                    <el-table
                        :data="tableData"
                        border
                        class="table"
                        ref="multipleTable"
                        header-cell-class-name="table-header"
                        @selection-change="handleSelectionChange"
                    >
                        <el-table-column type="selection" width="55" align="center"></el-table-column>
                        <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                        <el-table-column label="超管" align="center" width="85">
                            <template slot-scope="scope">
                                <el-button :type="scope.row.is_superuser=='1'?'success':(scope.row.is_superuser=='0'?'danger':'')" :icon="scope.row.is_superuser=='1'?'el-icon-success':(scope.row.is_superuser=='0'?'el-icon-error':'')" circle></el-button>
                            </template>
                        </el-table-column>
                        <el-table-column prop="username" label="用户名" width="200"></el-table-column>
                        <el-table-column prop="email" label="邮箱"  width="300" ></el-table-column>
                        <el-table-column prop="phone" label="手机号"  width="200"></el-table-column>
                        <el-table-column label="是否激活" align="center" width="155">
                            <template slot-scope="scope">
                                <el-tag
                                    :type="scope.row.is_active=='1'?'success':(scope.row.is_active=='0'?'danger':'')"
                                >{{scope.row.is_active | active}}</el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column  label="用户权限" align="center">
                            <template slot-scope="scope">
                                <el-tag
                                :type="!scope.row.quanxian==''?'success':(scope.row.quanxian=='0'?'danger':'')"
                                >{{scope.row.quanxian | quanxian}}</el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column prop="create_time" label="注册时间"></el-table-column>
                        <el-table-column label="操作" width="180" align="center">
                            <template slot-scope="scope">
                                <el-button
                                    type="text"
                                    icon="el-icon-edit"
                                    @click="handleEdit(scope.row.username,scope.row.id)"
                                >编辑</el-button>
                                <el-button
                                    type="text"
                                    icon="el-icon-delete"
                                    class="red"
                                    @click="Guidang(scope.row.id)"
                                >归档</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <div class="pagination">
                        <el-pagination
                            background
                            layout="total, prev, pager, next"
                            :current-page="query.pageIndex"
                            :page-size="query.pageSize"
                            :total="pageTotal"
                            @current-change="handlePageChange"
                        ></el-pagination>
                    </div>
                </el-tab-pane>
                <el-tab-pane :label="`未审核`" name="second">
                    <template v-if="message === 'second'">
                        <el-table 
                        :data="NotDate" 
                        :show-header="true" 
                        style="width: 100%"
                        border
                        class="table"
                        ref="multipleTable"
                        header-cell-class-name="table-header"
                        >
                            <el-table-column type="selection" width="55" align="center"></el-table-column>
                            <el-table-column label="用户名">
                                <template slot-scope="scope">
                                    <span class="message-title">{{scope.row.username}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="create_time" width="150" label="注册时间"></el-table-column>
                            <el-table-column width="120" label="操作">
                                <template slot-scope="scope">
                                    <el-button type="danger" @click="handlePass(scope.row.id)">通过审核</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <div class="handle-row">
                            <el-button type="danger">全部通过</el-button>
                        </div>
                    </template>
                </el-tab-pane>
            </el-tabs>
        </div>
        

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.uname"></el-input>
                </el-form-item>
                <el-form-item label="授予权限">
                    <el-select v-model="form.quanxian" clearable placeholder="请选择">
                        <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                            </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { getuser,modifyuser,guidang,shouquan} from '../../api/api';
export default {
    name: 'basetable',
    data() {
        return {
            message:'first',
            options:[{
                value: '1',
                label: '收货'
                },
                {
                value: '2',
                label: '发货'
                },
                {
                value: '3',
                label: '收货+发货'
                },
                {
                value: '4',
                label: '客服'
                },
            ],
            query: {
                address: '',
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            NotDate: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {
                id:'',
                quanxian:'',
                uname:'',
            },
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            getuser(this.query).then(res => {
                // console.log(res);
                this.tableData = res.all_users;
                this.NotDate = res.is_not_active;
                // this.pageTotal = res.pageTotal || 50;
            });
        },
        // 用户通过审核
        handlePass(id){
            modifyuser({id:id}).then(res=>{
                if (res.code == 200) {
                    this.$message.success(res.message)
                    this.$router.go(0)
                }
                else{
                    this.$message.error(res.message)
                }
            })
        },
        // 用户归档
        Guidang(id){
            console.log(id)
            guidang({id:id}).then(res=>{
                if (res.code == 200) {
                    this.$message.success(res.message)
                    this.$router.go(0)
                }
                else{
                    this.$message.error(res.message)
                }
            })
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.success('删除成功');
                    this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            console.log(val)
            // this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(uname,id) {
            this.form.uname = uname;
            this.form.id = id;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            var params = {
                username:this.form.uname,
                quanxian:this.form.quanxian,
                id:this.form.id,
            }
            shouquan(params).then(res=>{
                if (res.code == 200) {
                    this.$message.success(res.message)
                    this.$router.go(0)
                }
                else{
                    this.$message.error(res.message)
                }
            })
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    },
    filters:{
        active(val){
            if (val) {
                return '已激活'
            }else{
                return '未激活'
            }
        },
        quanxian(val){
            if (val==1) {
                return '收货'
            }else if(val == 2){
                return '发货'
            }else if(val == 3){
                return '收货+发货'
            }else if(val == 4){
                return '客服'
            }else{
                return '暂无'
            }
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
